<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>判断两个日期时间段是否重叠</title>
  </head>
  <body>
    <script>
      const ranges = [
        ["2024-09-01 13:01", "2024-09-01 14:01"],
        ["2024-09-01 14:01", "2024-09-01 16:01"],
        ["2024-09-01 09:01", "2024-09-01 10:01"],
        ["2024-09-01 11:01", "2024-09-01 12:01"],
      ];
      function isComputeTwoDateOverlap(ranges) {
        // 转时间戳
        const toTime = ranges.map((item) =>
          item.map((date) => new Date(date).getTime())
        );
        const sortTime = toTime.slice().sort((a, b) => a[0] - b[0]); // 排序
        for (let i = 1; i < sortTime.length; i++) {
          // 判断是否重叠
          if (sortTime[i][0] < sortTime[i - 1][1]) {
            return true; //有重叠
          }
        }
        return false; //所有时间都没有重叠
      }

      let isOverlap = isComputeTwoDateOverlap(ranges);
      console.log(isOverlap);
    </script>
  </body>
</html>
